package com.JustTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.util.StringValueResolver;

/**
 * Created by mike on 18/2/1.
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
    public MyBeanFactoryPostProcessor() {
        super();
        System.out.println("no.1.........."+this.getClass().getName()+"-MyBeanFactoryPostProcessor");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

//        StringValueResolver test = new
        StringValueResolver valueResolver = new StringValueResolver() {
            @Override
            public String resolveStringValue(String strVal) {
//                String resolved = PropertySourcesPlaceholderConfigurer.this.ignoreUnresolvablePlaceholders?propertyResolver.resolvePlaceholders(strVal):propertyResolver.resolveRequiredPlaceholders(strVal);
//                if(PropertySourcesPlaceholderConfigurer.this.trimValues) {
//                    resolved = resolved.trim();
//                }
                if(strVal.equals("testtest")) {
                    return "test";
                }if(strVal.equals("test3")){
                    return "test34";
                }else {
                    return strVal;
                }
            }
        };
        configurableListableBeanFactory.addEmbeddedValueResolver(valueResolver);
        System.out.println("no.2.........."+this.getClass().getName()+"-postProcessBeanFactory");
    }
}
